<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入作者"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="插件名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入插件名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="官方文档" prop="website">
        <el-input
          v-model="queryParams.website"
          placeholder="请输入官方文档"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="插件包名" prop="jarName">
        <el-input
          v-model="queryParams.jarName"
          placeholder="请输入插件包名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['test:hot-plug:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['test:hot-plug:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['test:hot-plug:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['test:hot-plug:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="HotPlugList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template #default="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="作者" align="center" prop="author" :show-overflow-tooltip="true"/>
      <el-table-column label="官方文档" align="center" prop="website" :show-overflow-tooltip="true"/>
      <el-table-column label="插件包名" align="center" prop="jarName" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" width="110" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-tooltip content="详情" placement="top">
            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['test:hot-plug:detail']"></el-button>
          </el-tooltip>
          <el-tooltip content="编辑" placement="top">
            <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['test:hot-plug:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['test:hot-plug:remove']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <Dialog ref="dialog" @refresh="getList"></Dialog>
  </div>
</template>

<script setup name="HostPlug">
import { listHotPlug, delHotPlug } from "@/api/system/hotPlug";
import Dialog from "@/views/system/hotPlug/dialog";

const { proxy } = getCurrentInstance();

const HotPlugList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    author: null,
    name: null,
    website: null,
    jarName: null,
    status: null,
  },
});

const { queryParams } = toRefs(data);

/** 查询扩展插件列表 */
function getList() {
  loading.value = true;
  listHotPlug(queryParams.value).then(response => {
    HotPlugList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.qualifiedName);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs["dialog"].show();
}

/** 详情按钮操作 */
function handleDetail(row) {
  proxy.$refs["dialog"].show(row.qualifiedName, true);
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _qualifiedName = row.qualifiedName || ids.value
  proxy.$refs["dialog"].show(_qualifiedName);
}

	
/** 删除按钮操作 */
function handleDelete(row) {
  const _qualifiedNames = row.qualifiedName || ids.value;
  proxy.$modal.confirm('是否确认删除扩展插件编号为"' + _qualifiedNames + '"的数据项？').then(function() {
    loading.value = true;
    return delHotPlug(_qualifiedNames);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/hotPlug/export', {
    ...queryParams.value
  }, `扩展插件_${new Date().getTime()}.xlsx`)
}

getList();
</script>
